﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Windows.Forms;
using DevExpress.Data.Filtering.Helpers;
using TheHeGeo.Util;
using TheHeGeoDao;

namespace TheHeGeo.UserControl
{
    public partial class UcNsdManager : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadNsd();
                LoadGrvData();
            }
            else
            {
                grvData.DataSource = Session[SessionConst.SES_GRV_ALL_NSD_IN_DA];
            }
        }

        protected void LoadNsd()
        {
            string str = ((SessionUser) Session[SessionConst.SES_USER]).NguoiSuDung.UserName;
            var nsd =DaoHelper.Entities.NguoiSuDungs.FirstOrDefault(p => p.UserName == str);
            Session[SessionConst.SES_USER] = new SessionUser() {NguoiSuDung = nsd};
            if (nsd != null)
            {
                txtUserName.Text = nsd.UserName;
                txtEmail.Text = nsd.Email;
                txtPhone.Text = nsd.DienThoai;
                txtHoTen.Text = nsd.HoTen;
                txtDcNhaRieng.Text = nsd.DiaChiNhaRieng;
                txtDcCty.Text = nsd.DiaChiCongTy;
                txtChucVu.Text = nsd.ChucVu;
                txtTenCty.Text = nsd.TenCongTy;
            }

        }

        protected void LoadGrvData()
        {
            var nsd=((SessionUser) Session[SessionConst.SES_USER]).NguoiSuDung.UserName;
            var dsDuAn = (from p in DaoHelper.Entities.PhanQuyenDuAns
                              join d in DaoHelper.Entities.DuAns on p.MaDuAn equals d.MaDuAn
                              where p.UserName==nsd select d);
            var listNsd=new List<object>();
            foreach (var p in dsDuAn)
            {
                var mada = p.MaDuAn;
                var dsNsd = DaoHelper.Entities.PhanQuyenDuAns.Where(a => a.MaDuAn == mada);
                foreach (var k in dsNsd)
                {
                    var temp = k.UserName;
                    var maTenDa = p.MaDuAn +" - "+ p.TenDuAn;
                    var h = (from b in DaoHelper.Entities.NguoiSuDungs
                        where (b.UserName == temp)
                        select new
                        {
                            MaTenDuAn = maTenDa,
                            b.UserName,
                            b.DienThoai,
                            b.Email,
                            b.HoTen
                        });
                    listNsd.AddRange(h);
                }
            }
            Session[SessionConst.SES_GRV_ALL_NSD_IN_DA] = listNsd;
            grvData.DataSource = listNsd;
            grvData.DataBind();
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            ASPxPopupControl1.ShowOnPageLoad = true;
        }

        protected void btCancelChangePwd_Click(object sender, EventArgs e)
        {
            ASPxPopupControl1.ShowOnPageLoad = false;
        }

        protected void btChangePwd_Click(object sender, EventArgs e)
        {
            lblChangePwdErr.Visible = false;
            if (!txtNewPwd1.Text.Equals(txtNewPwd2.Text))
            {
                lblChangePwdErr.Visible = true;
                lblChangePwdErr.Text = "Mật khẩu mới và nhập lại mật khẩu mới phải giống nhau";
                return;
            }
            var nsd = DaoHelper.Entities.NguoiSuDungs.FirstOrDefault(p => p.UserName == txtUserName.Text);
            if (nsd == null)
            {
                lblChangePwdErr.Visible = true;
                lblChangePwdErr.Text = "Người sử dụng không tồn tại";
                return;
            }
            if (!nsd.Password.Equals(UserHelper.UserPwdEncrypt(txtOldPwd.Text)))
            {
                lblChangePwdErr.Visible = true;
                lblChangePwdErr.Text = "Mật khẩu cũ không chính xác";
            }
            else
            {
                nsd.Password = UserHelper.UserPwdEncrypt(txtOldPwd.Text);
                DaoHelper.Entities.SaveChanges();
            }
        }

        protected void btOk_Click(object sender, EventArgs e)
        {
            lblErr.Visible = false;
            var email = DaoHelper.Entities.NguoiSuDungs.FirstOrDefault(p => p.Email == txtEmail.Text);
            if (email != null)
            {
                lblErr.Text = "Email đã được sử dụng, vui lòng dùng email khác";
                lblErr.Visible = true;
            }
            var nsd = DaoHelper.Entities.NguoiSuDungs.FirstOrDefault(p => p.UserName == txtUserName.Text);
            if (nsd != null)
            {
                nsd.Email = txtEmail.Text;
                nsd.DienThoai = txtPhone.Text;
                nsd.TenCongTy = txtTenCty.Text;
                nsd.HoTen = txtHoTen.Text;
                nsd.ChucVu = txtChucVu.Text;
                nsd.DiaChiNhaRieng = txtDcNhaRieng.Text;
                nsd.DiaChiCongTy = txtDcCty.Text;
                DaoHelper.Entities.SaveChanges();
                Session[SessionConst.SES_USER] = new SessionUser() {NguoiSuDung = nsd};
            }
        }
    }
}